package com.ctshk.sapp.order.bus.controller;


import java.math.BigDecimal;
import java.util.List;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.req.OperationByIdReq;
import com.ctshk.common.req.OperationPageByIdReq;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.bus.dto.BusOrderCollectionDTO;
import com.ctshk.rpc.order.bus.dto.BusOrderDetailDTO;
import com.ctshk.rpc.order.bus.dto.BusOrderInsuranceDTO;
import com.ctshk.rpc.order.bus.dto.BusOrderPageDTO;
import com.ctshk.rpc.order.bus.dto.BusOrderPayInfoDTO;
import com.ctshk.rpc.order.bus.dto.BusOrderRefundDTO;
import com.ctshk.rpc.order.bus.dto.BusOrderTouristDTO;
import com.ctshk.rpc.order.bus.req.BusBusOrderConfirmReq;
import com.ctshk.rpc.order.bus.req.BusCancelOrderReq;
import com.ctshk.rpc.order.bus.req.BusOrderCancelB2CReq;
import com.ctshk.rpc.order.bus.req.BusOrderCreateB2CReq;
import com.ctshk.rpc.order.bus.req.BusOrderOrderInformTouristReq;
import com.ctshk.rpc.order.bus.req.BusOrderPageReq;
import com.ctshk.rpc.order.bus.req.BusOrderPayB2CReq;
import com.ctshk.rpc.order.bus.req.BusOrderPaymentReq;
import com.ctshk.rpc.order.bus.req.BusOrderTouristReq;
import com.ctshk.rpc.order.bus.req.BusPayOrderCollectionReq;
import com.ctshk.rpc.order.bus.req.BusPlaceOrderReq;
import com.ctshk.rpc.order.bus.service.IBusOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 巴士订单表 前端控制器
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-02-24
 */
@Api(value="预订-订单中心-订单操作", tags="预订-订单中心-订单操作")
@RestController
@RequestMapping("/bus-order")
public class BusOrderController extends BaseController {


    @DubboReference
    IBusOrderService busOrderService;


    @ApiOperation(value="巴士订单-填写订单信息", notes="巴士订单-填写订单信息")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "填写订单信息", operDesc = "填写订单信息")
    @PostMapping("/save")
    public Result<Long> saveBusOrder(@RequestBody @Valid BusPlaceOrderReq busPlaceOrderReq) {
        TokenUser user = getCurrentUser(getRequest());
        return busOrderService.addBusOrder(busPlaceOrderReq,getCurrentUserId());
    }
    
    @ApiOperation("（测试用）B2C填写订单信息")
    @PostMapping("addBusOrderB2C")
    public Result<Long> addBusOrderB2C(@RequestBody BusOrderCreateB2CReq req) {
    	return busOrderService.addBusOrderB2C(req, 123l);
    }

    @ApiOperation(value = "巴士订单-订单中心-支付订单查询详情", notes = "巴士订单-订单中心-支付订单查询详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "巴士订单-订单中心-支付订单查询详情", operDesc = "巴士订单-订单中心-支付订单查询详情")
    @GetMapping("/queryPayinfo/{id}")
    public Result<BusOrderPayInfoDTO> paymentInfoById(@PathVariable("id") Long id) {
        return busOrderService.paymentInfoById(id);
    }


    @ApiOperation(value = "预订中心巴士票-支付页面-下单", notes = "预订中心巴士票-支付页面-下单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "预订中心巴士票-支付页面-下单", operDesc = "预订中心巴士票-支付页面-下单")
    @PostMapping(value = "/pay/{id}")
    public Result payOrder(@PathVariable("id") Long id,@RequestBody List<BusPayOrderCollectionReq> reqList){
        TokenUser currentUser = getCurrentUser(getRequest());
        return busOrderService.payOrder(id,reqList,currentUser);
    }
    
    @ApiOperation("预订中心巴士票-支付")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "预订中心巴士票-支付", operDesc = "预订中心巴士票-支付")
    @PostMapping(value = "payment")
    public Result<Long> payment(@RequestBody @Valid BusOrderPaymentReq req) {
		return busOrderService.payment(req, getCurrentUser());
    }
    
    @ApiOperation("（测试用）B2C支付订单")
    @PostMapping("payOrderB2C")
    public Result<Long> payOrderB2C(@RequestBody BusOrderPayB2CReq req) {
    	return busOrderService.payOrderB2C(req, 123l);
    }

    @ApiOperation(value = "订单中心巴士票--取消订单", notes = "订单中心巴士票-取消订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "订单中心巴士票--取消订单", operDesc = "订单中心巴士票--取消订单")
    @PostMapping(value = "/cancel/order")
    public Result cancelOrder(@RequestBody BusCancelOrderReq cancelOrderReq){
        TokenUser currentUser = getCurrentUser(getRequest());
        return busOrderService.cancelOrder(cancelOrderReq,currentUser);
    }
    
    @ApiOperation("（测试用）B2C取消订单")
    @PostMapping("cancelOrderB2C")
    public Result<Void> cancelOrderB2C(@RequestBody BusOrderCancelB2CReq req) {
    	return busOrderService.cancelOrderB2C(req, 123l);
    }

    @ApiOperation(value="订单中心-巴士票订单分页查询列表", notes="订单中心-巴士票订单分页查询列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单中心-巴士票订单分页查询列表", operDesc = "订单中心-巴士票订单分页查询列表")
    @PostMapping("/list")
    public Result<PageResponse<BusOrderPageDTO>> queryBusLineList(@RequestBody BusOrderPageReq busOrderPageReq) {
        PageResponse<BusOrderPageDTO> response = busOrderService.queyList(busOrderPageReq, getCurrentUser());
        return Result.success(response);
    }

    @ApiOperation(value = "订单中心-查询巴士订单详情", notes = "订单中心-查询巴士订单详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单中心-查询巴士订单详情", operDesc = "订单中心-查询巴士订单详情")
    @GetMapping(value = "/queryOrderDetailInfo/{orderId}")
    public Result<BusOrderDetailDTO> queryOrderDetailInfo(@PathVariable Long orderId){
        return busOrderService.queryOrderDetailInfo(orderId);
    }



    @ApiOperation(value = "订单中心巴士票--修改旅客信息", notes = "订单中心巴士票-修改旅客信息")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "订单中心巴士票--修改旅客信息", operDesc = "订单中心巴士票--修改旅客信息")
    @PostMapping(value = "/updateTourist")
    public Result updateTourist(@RequestBody BusOrderTouristReq touristReq){
        return busOrderService.updateBusOrderTourist(touristReq,getCurrentUser(getRequest()).getId());
    }

    @ApiOperation(value = "订单中心巴士票-订单操作-确认退款", notes = "订单中心巴士票-订单操作-确认退款")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "订单中心巴士票-订单中心-确认退款", operDesc = "订单中心巴士票-订单中心-确认退款")
    @PostMapping("/confirm/refund/{id}/{refundId}")
    public Result confirmRefund(@PathVariable("id") Long id, @PathVariable("refundId") Long refundId) {
        Long userId = getCurrentUser(getRequest()).getId();
        return busOrderService.confirmRefund(id, refundId, userId);
    }

    @ApiOperation("查询订单的旅客信息列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "查询订单的旅客信息列表", operDesc = "查询订单的旅客信息列表")
    @PostMapping("/queryTouristListByOrder")
    public Result<PageResponse<BusOrderTouristDTO>> queryTouristListByOrder(@RequestBody @Valid OperationPageByIdReq req) {
		return busOrderService.queryTouristListByOrder(req);
	}
    
    @ApiOperation("查询订单的收款明细信息列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "查询订单的旅客信息列表", operDesc = "查询订单的旅客信息列表")
    @PostMapping("/queryCollectionListByOrder")
    public Result<PageResponse<BusOrderCollectionDTO>> queryCollectionListByOrder(@RequestBody @Valid OperationPageByIdReq req) {
		return busOrderService.queryCollectionListByOrder(req);
	}
    
    @ApiOperation("查询订单的退款记录信息列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "查询订单的旅客信息列表", operDesc = "查询订单的旅客信息列表")
    @PostMapping("/queryRefundListByOrder")
    public Result<PageResponse<BusOrderRefundDTO>> queryRefundListByOrder(@RequestBody @Valid OperationPageByIdReq req) {
		return busOrderService.queryRefundListByOrder(req);
	}
    
    @ApiOperation("确认巴士订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "确认巴士订单", operDesc = "确认巴士订单")
    @PostMapping("confirmBusOrder")
    public Result<Void> confirmBusOrder(@RequestBody @Valid BusBusOrderConfirmReq req) {
		return busOrderService.confirmBusOrder(req, getCurrentUser());
	}
    
    @ApiOperation("查询订单的可退金额")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "查询订单的可退金额", operDesc = "查询订单的可退金额")
    @PostMapping("/findCanRefundAmountByOrder")
    public Result<BigDecimal> findCanRefundAmountByOrder(@RequestBody @Valid OperationByIdReq req) {
		return busOrderService.findCanRefundAmountByOrder(req);
	}
    
    @ApiOperation("巴士订单通知旅客")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "巴士订单通知旅客", operDesc = "巴士订单通知旅客")
    @PostMapping("/informTourist")
    public Result<Void> informTourist(@RequestBody @Valid BusOrderOrderInformTouristReq req) {
		return busOrderService.informTourist(req);
	}
    
    @ApiOperation("查询订单保险信息")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "巴士订单通知旅客", operDesc = "巴士订单通知旅客")
    @GetMapping("findInsuranceByOrder/{orderId}")
    public Result<BusOrderInsuranceDTO> findInsuranceByOrder(@PathVariable("orderId") Long orderId) {
		return busOrderService.findInsuranceByOrder(orderId);
	}
}
